package com.cheetah.service.impl;

import com.cheetah.builder.Result;
import com.cheetah.constants.MessageTypeEnums;
import com.cheetah.dao.MessageDAO;
import com.cheetah.dto.MessageDTO;
import com.cheetah.entity.GroupMessage;
import com.cheetah.entity.Message;
import com.cheetah.entity.User;
import com.cheetah.service.MessageService;
import com.cheetah.util.MapStructMapper;
import com.cheetah.vo.MessageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Administrator
 * @Date 2020/9/2
 * @Version V1.0
 * @Description:
 **/
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MapStructMapper mapStructMapper;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private MessageDAO messageDAO;

    @Override
    public MessageDTO sendMessage(MessageVO messageVO){
        Message message = mapStructMapper.messageVO2Message(messageVO);
        String groupId = String.valueOf(messageVO.getId());
        messageDAO.save(message);
        addGroupMessage(groupId,message);
        return mapStructMapper.message2MessageDTO(message);
    }

    @Override
    public Message sendGroupUserInviteMessage(List<String> userIds,String groupId){
        Long id = getMessageId();

        boolean groupMembersFlag = false;
        if(userIds.size() > 4){
            groupMembersFlag = true;
            userIds = userIds.stream().limit(4).collect(Collectors.toList());
        }
        List<User> users = getUsers(userIds);
        List<String> userNames = new ArrayList<>();
        users.forEach(u -> {
            userNames.add(u.getName());
        });

        String content = "";
        if(groupMembersFlag){
            content = "{}邀请" + StringUtils.join(userNames,",") + "等加入群聊";
        }else {
            content = "{}邀请" + StringUtils.join(userNames,",") + "加入群聊";
        }

        Message message = Message.builder()
                .id(id)
                .content(content)
                .type(MessageTypeEnums.SYSTEM)
                .build();

        messageDAO.save(message);

        addGroupMessage(groupId, message);

        return message;
    }

    private void addGroupMessage(String groupId, Message message) {
        GroupMessage groupMessage = GroupMessage.builder()
                .groupId(groupId)
                .msgId(message.getId())
                .score(message.getCreateTime().getTime())
                .build();
        messageDAO.save(groupMessage);
    }

    private List<User> getUsers(List<String> userIds) {
        String url = loadBalancerClient.choose("cheetah-user").getUri() + "/cheetah-user/user/get_users";
        Result<List<User>> result = restTemplate.postForObject(url,userIds, Result.class);;
        return result.getData();
    }

    private Long getMessageId() {
        String msgIdUrl = loadBalancerClient.choose("cheetah-id-generator") + "/id/generator/message/1000";
        return restTemplate.postForObject(msgIdUrl,null,Long.class);
    }
}
